package top.faig.blog.file;


import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Properties;

/**
 * <p>
 * 文件表 前端控制器
 * </p>
 *
 * @author fhs
 * @since 2020-04-27
 */
@RestController
@RequestMapping("/file")
@RequiredArgsConstructor
public class FileController {

    private final FileService fileService;

    /**
     * @api {POST}  /file/img  图片上传
     * @apiGroup file
     * @apiSuccess {file} picture 图片流
     * @apiSuccessExample {json} 成功
     * {
     * "msg":"success","code":0,"url":"http://localhost:9091/blog-api/img/20200429132327img_34f9f763a6e67595c34b0ce0c0fcace7.jpg"}
     */
    @PostMapping("/upload")
    public String upload(@RequestParam("file") MultipartFile file
                        ,@RequestParam(required = false, defaultValue = "/dafalut") String path) {

        return fileService.upload(file, path);
    }

    public static void main(String[] args) {
        Properties properties = System.getProperties();
        String property = properties.getProperty("os.name");
        String s = property.toUpperCase();
        System.out.println(properties);
        System.out.println(property);
        System.out.println(s);
        System.out.println(s.indexOf("WINDOWS") != -1);


    }

    public static String formatPath(String str, Boolean... types){
        if(str == null || str.equals("")) return str;

        Boolean type = true;
        if(types.length != 0 && !types[0]) type = false;

        String res = "";
        int index = -1;
        do {
            index = str.indexOf("/");
            System.out.println(index);
            if(index != -1){
                res += str.substring(0, index) + "\\";

                str = str.substring(index + 1, str.length() - 1);
                System.out.println("str:" + str);
            } else {
                res += str;
            }
        } while (index != -1);

        System.out.println(type);
        return res;
    }

}
